package com.cisdi.blog.web.admin;

import com.cisdi.blog.entity.Blog;
import com.cisdi.blog.entity.Tag;
import com.cisdi.blog.entity.User;
import com.cisdi.blog.service.BlogService;
import com.cisdi.blog.service.TagService;
import com.cisdi.blog.service.TypeService;
import com.cisdi.blog.vo.BlogQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author pengyangyan
 * @version 1.0.0
 * @date 2020年03月19日 10:42:00
 */
@Controller
@RequestMapping("/admin/blogs")
public class BlogController {

    @Autowired
    BlogService blogService;
    @Autowired
    TypeService typeService;
    @Autowired
    TagService tagService;


    @GetMapping
    public String blogs(@PageableDefault(size = 10,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                        BlogQuery query, Model model){
        model.addAttribute("page",blogService.listBlog(pageable,query));
        model.addAttribute("types",typeService.listType());
        return "admin/blogs";
    }

    @PostMapping("/search")
    public String search(@PageableDefault(size = 10,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                         BlogQuery query, Model model){
        model.addAttribute("page",blogService.listBlog(pageable,query));
        return "admin/blogs::blogList";
    }

    @GetMapping("/input")
    public String input(Model model){
        model.addAttribute("tags",tagService.listTag());
        model.addAttribute("types",typeService.listType());
        model.addAttribute("blog",new Blog());
        return "admin/postBlog";
    }

    @GetMapping("/{id}/input")
    public String input(@PathVariable Long id,Model model){
        model.addAttribute("tags",tagService.listTag());
        model.addAttribute("types",typeService.listType());
        Blog blog = blogService.getBlog(id);
        blog.init();
        model.addAttribute("blog",blog);
        return "admin/updateBlog";
    }

    @PostMapping("/post")
    public String post(Blog blog, RedirectAttributes attributes, HttpSession session){
        blog.setUser((User) session.getAttribute("user"));
        blog.setType(typeService.getType(blog.getType().getId()));
        if (StringUtils.isNotBlank(blog.getTagIds())){
            blog.setTags(tagService.listTag(blog.getTagIds()));
        }
        if (blog.getId()==null){
            Blog b = blogService.saveBlog(blog);
            if (b == null){
                attributes.addFlashAttribute("message","操作失败");
                attributes.addFlashAttribute("status",false);
            }else {
                attributes.addFlashAttribute("message","操作成功");
                attributes.addFlashAttribute("status",true);
            }
        }else {
            Blog b = blogService.updateBlog(blog.getId(), blog);
            if (b == null){
                attributes.addFlashAttribute("message","操作失败");
                attributes.addFlashAttribute("status",false);
            }else {
                attributes.addFlashAttribute("message","操作成功");
                attributes.addFlashAttribute("status",true);
            }
        }
        return "redirect:/admin/blogs";
    }

    @GetMapping("/{id}/delete")
    public String delete(@PathVariable("id") Long id,RedirectAttributes attributes){
        blogService.removeBlog(id);
        attributes.addFlashAttribute("message","操作成功");
        attributes.addFlashAttribute("status",true);
        return "redirect:/admin/blogs";
    }
}
